package com.example.demo.service.order.impl;

import com.alibaba.fastjson.JSON;
import com.example.demo.common.BaseErrorConfigEnum;
import com.example.demo.common.BasePageResponse;
import com.example.demo.common.ResultObject;
import com.example.demo.common.TerminologyConfigEnum;
import com.example.demo.dao.IEmailDao;
import com.example.demo.jooq.tables.daos.EmailDao;
import com.example.demo.jooq.tables.pojos.Email;
import com.example.demo.model.po.EmailPage;
import com.example.demo.model.request.EmailPageRequest;
import com.example.demo.model.request.EmailRequest;
import com.example.demo.model.response.EmailResponse;
import com.example.demo.service.BaseBusinessServiceImpl;
import com.example.demo.service.order.IEmailService;
import com.example.demo.util.AssertUtils;
import com.example.demo.util.ClazzUtils;
import com.example.demo.util.DateUtils;
import org.modelmapper.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class IEmailServiceImpl extends BaseBusinessServiceImpl implements IEmailService {
    @Autowired
    private IEmailDao iEmailDao;

    @Autowired
    private EmailDao emailDao;

    /**
     * 分页查询Vps
     * @param emailPageRequest 分页查询请求参数
     * @return ResultObject<BasePageResponse<VpsResponse>>
     */
    @Override
    public ResultObject<BasePageResponse<EmailResponse>> queryPageEmailList(EmailPageRequest emailPageRequest) {
        this.getLogger().info("查询邮箱列表,参数为:{}", JSON.toJSONString(emailPageRequest));
        ResultObject<BasePageResponse<EmailResponse>> responseResultObject = new ResultObject<>();
        List<EmailPage> vpsPages =  iEmailDao.queryPageEmailList(emailPageRequest);
        if(AssertUtils.isNotEmpty(vpsPages)){
            Integer integer =vpsPages.get(0).getTotalLine();
            List<EmailResponse> vpsResponses = (List<EmailResponse>)this.converterList(vpsPages,new TypeToken<List<EmailResponse>>() {}.getType() );
            //术语转换
            vpsResponses.forEach(visaResponse -> {
                if(AssertUtils.isNotNull(visaResponse.getEmailStatus())){
                    Arrays.stream(TerminologyConfigEnum.EMAIL_STATUS.values()).filter(value->value.name().equals(visaResponse.getEmailStatus())).forEach(value->visaResponse.setEmailStatus(value.desc()));
                }
            });
            BasePageResponse<EmailResponse> responseBasePageResponse =BasePageResponse.getData(emailPageRequest.getCurrentPage(),emailPageRequest.getPageSize(),integer,vpsResponses);
            responseResultObject.setData(responseBasePageResponse);
        }
        return responseResultObject;
    }

    @Override
    public ResultObject<EmailResponse> queryOneEmail(String emailId) {
        AssertUtils.isNotEmpty(this.getLogger(),emailId,"未找到邮箱ID");
        ResultObject<EmailResponse> resultObject = new ResultObject<>();
        Email email = emailDao.fetchOneByEmailId(emailId);
        if(AssertUtils.isNotNull(email)){
            EmailResponse emailResponse =(EmailResponse)this.converterObject(email,EmailResponse.class);
            resultObject.setData(emailResponse);
        }
        return resultObject;
    }

    /**
     * 保存邮箱信息
     * @param emailRequest 邮箱对象
     * @return ResultObject<BasePageResponse<VpsResponse>>
     */
    @Override
    public ResultObject saveEmail(EmailRequest emailRequest) {
        this.getLogger().info("保存邮箱,请求数据为:{}",JSON.toJSONString(emailRequest));
        ResultObject resultObject = new ResultObject();
        //参数校验
        vaildEmailParameter(emailRequest);
        Email email =null;
        if(AssertUtils.isNotNull(emailRequest.getEmailId())){
            email = emailDao.fetchOneByEmailId(emailRequest.getEmailId());
            AssertUtils.isNotNull(this.getLogger(),email, BaseErrorConfigEnum.EMAIL_NOT_NULL);
        }else {
            email=new Email();
        }
        ClazzUtils.copyProperties(emailRequest,email);
       //持久化
        if(AssertUtils.isNotEmpty(email.getEmailId())){
            email.setUpdatedDate(System.currentTimeMillis());
            emailDao.update(email);
        }else {
            email.setEmailStatus(AssertUtils.isNotNull(emailRequest.getEmailStatus())?emailRequest.getEmailStatus():TerminologyConfigEnum.EMAIL_STATUS.NEW.name());
            email.setEmailId(DateUtils.getJobNumberByTime2("EMAIL",null,DateUtils.FORMATE53,false));
            email.setCreatedDate(System.currentTimeMillis());
            emailDao.insert(email);
        }
        return resultObject;
    }

    private void vaildEmailParameter(EmailRequest emailRequest) {
        AssertUtils.isNotNull(this.getLogger(),emailRequest.getEmail(), BaseErrorConfigEnum.EMAIL_DETAIL_IS_NOT_NULL);
        AssertUtils.isNotNull(this.getLogger(),emailRequest.getAssistEmail(), BaseErrorConfigEnum.ASSET_EMAIL_IS_NOT_NULL);
        AssertUtils.isNotNull(this.getLogger(),emailRequest.getEmailPassword(), BaseErrorConfigEnum.PASSWORD_IS_NOT_NULL);
    }
}
